package com.cnap.db.handler;

import com.cnap.model.strategy.schedule.LoadStrategyDTO;
import com.cnap.utils.JsonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * commonScheduleDTO handler
 *
 * @author xc
 **/
public class LoadStrategyDTOHandler implements TypeHandler<LoadStrategyDTO> {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, LoadStrategyDTO commonScheduleDTO, JdbcType jdbcType) throws SQLException {
        String str = JsonUtil.objectToJsonStr(commonScheduleDTO);
        preparedStatement.setString(i, str);
    }

    @Override
    public LoadStrategyDTO getResult(ResultSet resultSet, String s) throws SQLException {
        String jsonStr = resultSet.getString(s);
        return JsonUtil.jsonStrToObject(jsonStr, new TypeReference<LoadStrategyDTO>() {
        });
    }

    @Override
    public LoadStrategyDTO getResult(ResultSet resultSet, int i) throws SQLException {
        String jsonStr = resultSet.getString(i);
        return JsonUtil.jsonStrToObject(jsonStr, new TypeReference<LoadStrategyDTO>() {
        });
    }

    @Override
    public LoadStrategyDTO getResult(CallableStatement callableStatement, int i) throws SQLException {
        String jsonStr = callableStatement.getString(i);
        return JsonUtil.jsonStrToObject(jsonStr, new TypeReference<LoadStrategyDTO>() {
        });
    }
}
